﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Refraction
{
    public class RefractionMask : Actor
    {
        #region Variables
        private Effect _refractionEffect;  //effect for refractioning
        private Texture2D _displacementMap;
        private float _refractionScale = 0.1f;
        private QuadRenderComponent quadRenderer;

        public bool IsDrawingRefraction = false;
        public RenderTarget2D Scene;
        public RenderTarget2D AlphaMask;
        #endregion

        //Ctor
        public RefractionMask(Game game, Vector3 position) : base(game, position)
        {
            quadRenderer = new QuadRenderComponent(Game);
            Game.Components.Add(quadRenderer);
        }

        protected override void LoadContent()
        {
            _effect = Game.Content.Load<Effect>("Shaders/RefractionMaskEffect"); //is used to make some previous necesarry work
            _model = Game.Content.Load<Model>("Models/ball");  
            _refractionEffect = Game.Content.Load<Effect>("Shaders/RefractioningMaskEffect");
            _displacementMap = Game.Content.Load<Texture2D>("Textures/bumpMap");

            _refractionEffect.Parameters["DisplacementMap"].SetValue(_displacementMap);

            Scene = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height,
                false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
            AlphaMask = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height,
                false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            _refractionScale = MathHelper.Clamp(_refractionScale, 0, 1);

            _effect.Parameters["View"].SetValue(Camera.view);
            _effect.Parameters["Projection"].SetValue(Camera.projection);

            _refractionEffect.Parameters["RefractionScale"].SetValue(RefractionScale);
            _refractionEffect.Parameters["View"].SetValue(Camera.view);
            _refractionEffect.Parameters["Projection"].SetValue(Camera.projection);

            base.Update(gameTime);
        }



        public override void Draw(GameTime gameTime)
        {
            if (IsDrawingRefraction)
            {
                Matrix world = Matrix.CreateScale(5f) * Matrix.CreateTranslation(Position);
                _refractionEffect.Parameters["SceneTexture"].SetValue(Scene);
                _refractionEffect.Parameters["AlphaMask"].SetValue(AlphaMask);

                DrawModel(world, _refractionEffect);
            }
            else
            {
                Matrix world = Matrix.CreateScale(5f)*Matrix.CreateTranslation(Position);

                DrawModel(world);
            }
            base.Draw(gameTime);
        }

        //Extra draw method for refraction
        //public void Draw(RenderTarget2D scene, RenderTarget2D alphaMask)
        //{    
        //    _refractionEffect.Parameters["SceneTexture"].SetValue(scene);
        //    _refractionEffect.Parameters["AlphaMask"].SetValue(alphaMask);

        //    _refractionEffect.Techniques[0].Passes[0].Apply();
        //    quadRenderer.Render(Vector2.One * -1, Vector2.One);   
        //}

        public void Draw(RenderTarget2D scene, RenderTarget2D alphaMask)
        {
            Matrix world = Matrix.CreateScale(5f) * Matrix.CreateTranslation(Position);
            _refractionEffect.Parameters["SceneTexture"].SetValue(scene);
            _refractionEffect.Parameters["AlphaMask"].SetValue(alphaMask);

            DrawModel(world, _refractionEffect);
        }
    }
}
